package com.wolfword.crawler.server.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.wolfword.common.util.password.GenerateShortUrl;
import com.wolfword.crawler.server.dao.CrawlTaskDao;
import com.wolfword.crawler.server.dao.RootUrlInfoDao;
import com.wolfword.crawler.server.entity.CrawlTaskEntity;
import com.wolfword.crawler.server.entity.RootUrlInfoEntity;
import com.wolfword.crawler.server.service.RootUrlInfoService;

@Service
public class RootUrlInfoServiceImpl implements RootUrlInfoService {
	
	private final Logger logger = LoggerFactory.getLogger(RootUrlInfoServiceImpl.class);
	
	@Autowired
	private RootUrlInfoDao rootUrlInfoDao;
	@Autowired
	private CrawlTaskDao crawlTaskDao;

	public RootUrlInfoDao getRootUrlInfoDao() {
		return rootUrlInfoDao;
	}

	@Resource
	public void setRootUrlInfoDao(RootUrlInfoDao rootUrlInfoDao) {
		this.rootUrlInfoDao = rootUrlInfoDao;
	}

	/**
	 * 插入数据
	 *
	 * @param entity
	 *            实体类
	 * @return 主键值
	 */
	@Override
	public long insert(RootUrlInfoEntity entity) {
		rootUrlInfoDao.insert(entity);
		return entity.getId();
	}

	/**
	 * 批量插入数据
	 *
	 * @param list
	 *            实体类
	 */
	@Override
	public void batchInsert(List<RootUrlInfoEntity> list) {
		rootUrlInfoDao.batchInsert(list);
	}

	/**
	 * 根据主键更新数据
	 *
	 * @param entity
	 *            新数据
	 */
	@Override
	public Integer updateByPrimary(RootUrlInfoEntity entity) {
		return rootUrlInfoDao.updateByPrimary(entity);
	}

	/**
	 * 根据主键获取数据
	 *
	 * @param entity
	 *            查询条件
	 * @return 查询结果
	 */
	@Override
	public RootUrlInfoEntity getByPrimary(RootUrlInfoEntity entity) {
		return rootUrlInfoDao.getByPrimary(entity);
	}

	/**
	 * 根据查询条件获取一条记录
	 *
	 * @param conditions
	 *            查询条件
	 * @return 查询结果
	 */
	@Override
	public RootUrlInfoEntity getOne(Map<String, Object> conditions) {
		return rootUrlInfoDao.getOne(conditions);
	}

	/**
	 * 根据查询条件查询
	 *
	 * @param conditions
	 *            查询条件
	 * @return 结果集
	 */
	@Override
	public List<RootUrlInfoEntity> queryList(Map<String, Object> conditions) {
		return rootUrlInfoDao.queryList(conditions);
	}

	/**
	 * 查询所有数据
	 *
	 * @return 结果集
	 */
	@Override
	public List<RootUrlInfoEntity> getAll() {
		return rootUrlInfoDao.getAll();
	}
	
	/**
	 * list
	 * @param url
	 * @param containStr
	 * @param status
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 * @author ServerZhang
	 * @date 2018年1月17日
	 */
	@Override
	public List<RootUrlInfoEntity> list(String url, String orgName, String containStr,
			Integer status,Integer pageIndex,Integer pageSize) {
		PageHelper.startPage(pageIndex, pageSize);
		return rootUrlInfoDao.list(url,orgName, containStr, status);
	}
	
	/**
	 * 添加到任务
	 * @param ids
	 * @author ServerZhang
	 * @date 2018年1月17日
	 */
	@Override
	@Transactional
	public void add2task(List<Long> ids){
		List<CrawlTaskEntity> taskEntities = new ArrayList<CrawlTaskEntity>();
		List<RootUrlInfoEntity> list4Ids = rootUrlInfoDao.list4Ids(ids);
		Iterator<RootUrlInfoEntity> iterator = list4Ids.iterator();
		while (iterator.hasNext()) {
			CrawlTaskEntity entity = new CrawlTaskEntity();
			RootUrlInfoEntity next = iterator.next();
			entity.setUrl(next.getUrl());
			entity.setUrlShort(GenerateShortUrl.shortUrl(next.getUrl()));
			entity.setContainStr(next.getContainStr());
			entity.setRootUrlId(next.getId());
			taskEntities.add(entity);
		}
		
		crawlTaskDao.batchInsert(taskEntities);
		
		rootUrlInfoDao.changeStatus(1, ids);
	}
	/**
	 * 插入并添加到任务
	 * @param entity
	 * @author ServerZhang
	 * @date 2018年1月24日
	 */
	@Override
	public void insertAnd2Task(RootUrlInfoEntity entity){
		rootUrlInfoDao.insert(entity);
		Long id = entity.getId();
		if (id != null) {
			CrawlTaskEntity taskEntity = new CrawlTaskEntity();
			taskEntity.setUrl(entity.getUrl());
			taskEntity.setUrlShort(GenerateShortUrl.shortUrl(entity.getUrl()));
			taskEntity.setContainStr(entity.getContainStr());
			taskEntity.setRootUrlId(id);
			crawlTaskDao.insert(taskEntity);
			ArrayList<Long> ids = new ArrayList<Long>();
			ids.add(id);
			rootUrlInfoDao.changeStatus(1, ids);
		}
	}
	/**
	 * 将根网址表中已添加到任务且所有任务都已经完成的的数据修改状态为完成
	 * @author ServerZhang
	 * @date 2018年1月25日
	 */
	@Override
	public void rootUrlStatistic(){
		try {
			rootUrlInfoDao.rootUrlStatistic();
		} catch (Exception e) {
			logger.error("更新根网址状态出错！");
			return;
		}
	}
}
